#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <stdio.h>
int main(){

  extern char** environ;

  pid_t id = fork();
  if(id == 0){
    printf("child:%d\n",getpid());
    ////成功
    //execl("/bin/ls","ls","-a","-l",NULL);
    //失败
    //execl("/bin/ls1111111","ls","-a","-l",NULL);
    
    //用自己写的函数进行替换
    ////自定义环境变量
    //const char* myenv[]={"MYENV=thismyenv", NULL};
    //execl("./other/othertest", "othertest",NULL);
    //execle("./other/othertest", "othertest", NULL, myenv); 
    
    //添加环境变量
    putenv("MYENV=NEWENV");
    //传父进程的环境变量
    execle("./other/othertest", "othertest", NULL, environ); 
    printf("child fail\n");
    exit(1);
  }else{
    sleep(5);
    int status = 0;
    
    printf("parent:%d\n",getpid());
    waitpid(id,&status,0);
    printf("child exit code:%d\n",WEXITSTATUS(status));
  }
  return 0;
}
